// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <QApplication>
#include <iostream>

#include "OpenMeca/Core/SelectionManager.hpp"
#include "OpenMeca/Gui/WidgetSelectItem.hpp"
#include "OpenMeca/Util/Dimension.hpp"
#include "OpenMeca/Util/Unit.hpp"

namespace OpenMeca
{
  namespace Gui
  {
   
    AbstractWidgetSelectItem::AbstractWidgetSelectItem(QWidget* parent)
      :QWidget(parent)
    {
      QObject::connect(&Core::Singleton<Core::SelectionManager>::Get(), SIGNAL(ItemSelected(Core::UserItem&)), 
			this, SLOT(ItemSelected(Core::UserItem&)));




      horizontalLayout_ = new QHBoxLayout(this);
      label_ = new QLabel(this);
      comboBox_ = new QComboBox(this);
      horizontalLayout_->addWidget(label_);
      horizontalLayout_->addWidget(comboBox_);
    }

    AbstractWidgetSelectItem::~AbstractWidgetSelectItem()
    {
      QObject::disconnect(&Core::Singleton<Core::SelectionManager>::Get(), 0, this, 0);      
    }

    void
    AbstractWidgetSelectItem::ItemSelected(Core::UserItem& item)
    {
      ItemSelected_CallBack(item);
    }

    void 
    AbstractWidgetSelectItem::ClearComboBox()
    {
      while (comboBox_->count() > 0)
      {
	comboBox_->removeItem(0);
      }
    }

     QLabel& 
     AbstractWidgetSelectItem::GetLabel()
     {
       return *label_;
     }
  
     QComboBox& 
     AbstractWidgetSelectItem::GetComboBox()
     {
       return *comboBox_;
     }

     void 
     AbstractWidgetSelectItem::SetLabel(const QString& str)
     {
       label_->setText(str);
     }


    void 
    AbstractWidgetSelectItem::showEvent(QShowEvent *event)
    {
      std::cout << "AbstractWidgetSelectItem::showEvent" << std::endl;
      QObject::connect(&Core::Singleton<Core::SelectionManager>::Get(), SIGNAL(ItemSelected(Core::UserItem&)), 
		       this, SLOT(ItemSelected(Core::UserItem&)));
      
      QWidget::showEvent(event);
    }


    void 
    AbstractWidgetSelectItem::hideEvent(QHideEvent *event)
    {
      std::cout << "AbstractWidgetSelectItem::hideEvent" << std::endl;
      QObject::disconnect(&Core::Singleton<Core::SelectionManager>::Get(), 0, this, 0);
      QWidget::hideEvent(event);
    }



    
    
  }
}


